package org.jeecg.modules.demo.crm.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.demo.crm.dto.ChartDataDto;
import org.jeecg.modules.demo.crm.dto.IndexListDto;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * @Description: 子项目信息表
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
public interface CSubProjectMapper extends BaseMapper<CSubProject> {



    @Select("select * from c_sub_project where id= #{subProjectId} and del_flag =0 limit 1")
    CSubProject findSubProject(@Param("subProjectId") String subProjectId);


    @Select("select count(id) from  c_sub_project where  project_id= #{projectId} and del_flag =0 ")
    Integer subcontrCount(@Param("projectId") String projectId);


    @Select("select * from c_sub_project where project_id= #{projectId}")
    List<CSubProject> findByProjectId(@Param("projectId") String projectId);

    @Select("<script> select * from c_sub_project where project_id= #{projectId}  <if test='subProjectIds != null and subProjectIds.size() > 0'>  AND id IN <foreach collection='subProjectIds' item='id' open='(' separator=',' close=')'>#{id}</foreach> </if> </script>")
    List<CSubProject> findByProjectIdAndId(@Param("projectId") String projectId ,@Param("subProjectIds") List<String> subProjectIds);

    @Select("select count(*)  from c_sub_project where project_id= #{projectId}  ")
    Integer sumCount(@Param("projectId") String projectId);

    @Select("select count(*)  from c_sub_project where create_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01') AND create_time <= DATE_FORMAT(LAST_DAY(CURDATE()), '%Y-%m-%d') and project_id= #{projectId}  ")
    Integer sumMonthCount(@Param("projectId") String projectId);

    @Select("SELECT DATE_FORMAT(create_time, '%Y-%m') AS 'name', COUNT(*) AS 'value' FROM c_sub_project WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL #{day} MONTH) AND project_id = #{projectId} GROUP BY DATE_FORMAT(create_time, '%Y-%m') ORDER BY name DESC;")
    List<ChartDataDto> getChart(@Param("projectId") String projectId, @Param("day") Integer day);

    @Select("select sub_project_name as 'name',total_quotation as 'total'  from c_sub_project where project_id= #{projectId}  order by total_quotation desc ")
    List<IndexListDto> subProjectList(@Param("projectId") String projectId);


    @Select("select * from c_sub_project where contact_id= #{contactId}")
    List<CSubProject> findByContractorId(@Param("contactId") String contactId);

    @Select("select * from c_sub_project where status = '报价中'")
    List<CSubProject> subProjectQuote();

    @Select("SELECT  GROUP_CONCAT(DISTINCT t2.project_id)  from c_contractor_info as t1  LEFT JOIN c_sub_project as t2 on t1.id = t2.contact_id where t1.user_id =#{userId}")
    String projectPermissions(@Param("userId") String userId);


    @Select("SELECT  GROUP_CONCAT(DISTINCT t2.id)  from c_contractor_info as t1  LEFT JOIN c_sub_project as t2 on t1.id = t2.contact_id where t1.user_id =#{userId}")
    String subProjectPermissions(@Param("userId") String userId);



    IPage<CSubProject> selectSubProjectWithConditions(
            Page<CSubProject> page,
            @Param("subProjectIds") List<String> subProjectIds,
            @Param("projectId") String projectId,
            @Param("libraryIds") List<String> libraryIds,
            @Param("title") String title
    );

    @Select("select * from c_sub_project where status = #{status} AND ( create_time >= DATE_SUB(NOW(), INTERVAL 1 DAY)OR update_time >= DATE_SUB(NOW(), INTERVAL 1 DAY))")
    List<CSubProject> gather(@Param("status") String status);

    @Delete("delete from c_contract where sub_project_id =#{subProjectId}")
    void deleletContract(@Param("subProjectId")String subProjectId);

    @Delete("delete from c_project_quote where sub_project_id =#{subProjectId}")
    void deleteProjectQuote(@Param("subProjectId")String subProjectId);

    @Delete("delete from c_payment_request where sub_project_id =#{subProjectId}")
    void deletePaymentRequest(@Param("subProjectId")String subProjectId);

    @Delete("delete from c_payment_invoice where sub_project_id =#{subProjectId}")
    void deletePaymentInvoice(@Param("subProjectId")String subProjectId);

    @Update("update c_sub_project set additional_id=null where additional_id=#{additionalId}")
    void unbindingAdditionalId(@Param("additionalId")String additionalId);
}
